﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HZH_Controls.ViewModel;

namespace HZH_Controls.Controls.KeyBord
{
    public partial class SinglePhraseSelect : UserControl
    {
        public List<PhraseViewModel> Data = new List<PhraseViewModel>();
        public Action<string, string, string> mCall = null;
        public TextBox ParentTxt { get; set; }
        public SinglePhraseSelect()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.SupportsTransparentBackColor,
            true
            );
            this.BackColor = Color.FromArgb(0, 0, 0, 0);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.DrawString(
            "test"
            ,
            new
            Font(
            "Tahoma"
            , 8.25f), Brushes.Red,
            new
            PointF(20, 20));


        }

        private void SinglePhraseSelect_Load(object sender, EventArgs e)
        {
            ListAll.DataSource = Data;
            ListAll.DisplayMember = "Name";
            if (ListAll.Items.Count > 0)
            {
                ListAll.SelectedIndex = 0;
            }
            if (ParentTxt != null)
            {
                ParentTxt.TextChanged += txtInput_TextChanged;
                ParentTxt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtSearch_KeyDown);
            }
        }

        private void ListAll_Click(object sender, EventArgs e)
        {
            ListBox eObj = sender as ListBox;
            PhraseViewModel item = eObj.SelectedItem as PhraseViewModel;
            if (item != null)
            {
                if (ParentTxt != null)
                {
                    ParentTxt.Text = item.Name;
                }
                this.Parent.Hide();
                if (mCall != null)
                {
                    mCall(item.Name, item.Code, string.Empty);
                }
            }
            else
            {
                this.Parent.Hide();
            }

        }

        private void ListAll_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            string item = (sender as ListBox).Items[e.Index] as string;
            e.Graphics.DrawString(item, e.Font, new SolidBrush(e.ForeColor),
                                    e.Bounds);
            e.DrawBackground();
            e.DrawFocusRectangle();
            PhraseViewModel model = ListAll.Items[e.Index] as PhraseViewModel;

            if (model != null)
            {
                //让文字位于Item的中间
                float difH = (e.Bounds.Height - e.Font.Height) / 2;
                RectangleF rf = new RectangleF(e.Bounds.X, e.Bounds.Y + difH, e.Bounds.Width, e.Font.Height);
                e.Graphics.DrawString(model.Name, e.Font, new SolidBrush(e.ForeColor), rf);
            }
        }

        private void ListAll_MouseMove(object sender, MouseEventArgs e)
        {
            ListBox list = sender as ListBox;
            list.SelectedIndex = list.IndexFromPoint(e.Location);
        }

        void txtInput_TextChanged(object sender, EventArgs e)
        {
            string val = ParentTxt.Text;
            if (val.Length > 0)
            {
                var list = Data.Where(p => p.Name.Contains(val) || p.PinYin.ToUpper().Contains(val.ToUpper())).ToList();
                ListAll.DataSource = list;
                ListAll.DisplayMember = "Name";
                if (ListAll.Items.Count > 0)
                {
                    ListAll.SelectedIndex = 0;
                }

            }
            else
            {
                ListAll.DataSource = Data;
                ListAll.DisplayMember = "Name";
                if (ListAll.Items.Count > 0)
                {
                    ListAll.SelectedIndex = 0;
                }
            }

        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (ListAll.SelectedIndex >= 0)
                {
                    this.Parent.Hide();
                    if (mCall != null)
                    {
                        var item = ListAll.SelectedItem as PhraseViewModel;
                        if (item != null)
                        {
                            if (ParentTxt != null)
                            {
                                ParentTxt.Text = item.Name;
                            }
                            mCall(item.Name, item.Code, string.Empty);
                        }
                    }
                }
            }
            if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left)
            {
                if (ListAll.SelectedIndex > 0)
                    ListAll.SelectedIndex--;
            }
            else if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right)
            {
                if (ListAll.SelectedIndex < ListAll.Items.Count - 1)
                    ListAll.SelectedIndex++;
            }
        }
    }
}
